package model;

import java.util.Calendar;

/**
 * 
 * Class voor mooi uitziende string van Calendar bijvoorbeeld: 5 dagen ,1 uur
 * ,10 minuten en ,3 seconden
 */
public class CalendarFormatter
{

	public static String getTimeAgo(Calendar calendar)
	{
		Calendar now = Calendar.getInstance();
		long mseconds = now.getTimeInMillis() - calendar.getTimeInMillis();
		return getNiceLookingTime(mseconds) + " geleden";
	}

	public static String getTimeLeft(Calendar calendar)
	{
		Calendar now = Calendar.getInstance();
		long mseconds = calendar.getTimeInMillis() - now.getTimeInMillis();
		if (mseconds < 0)
			return "GESLOTEN";
		return "nog " + getNiceLookingTime(mseconds) + " open";
	}

	public static String getNiceLookingTime(Long mseconds)
	{
		StringBuffer result = new StringBuffer();
		long seconds = mseconds / 1000;
		long days = seconds / 86400;
		long hours = (seconds - (days * 86400)) / 3600;
		long minutes = (seconds - (days * 86400) - (hours * 3600)) / 60;
		seconds = seconds - (days * 86400) - (hours * 3600) - minutes * 60;

		if (days > 0)
			result.append(days + (days == 1 ? " dag " : " dagen "));
		if (hours > 0)
		{
			if (result.length() > 1)
				result.append(",");
			result.append(hours + (hours == 1 ? " uur " : " uren "));
		}
		if (minutes > 0)
		{
			if (result.length() > 1)
				result.append(",");
			result.append(minutes + (minutes == 1 ? " minuut " : " minuten "));
		}
		if (seconds > 0)
		{
			if (result.length() > 1)
				result.append(" en ");
			result.append(seconds + (seconds == 1 ? " seconde " : " seconden "));
		}

		return result.toString();
	}
}
